package com.xzmzhp.controller.maintain;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.maintain.Lend;
import com.xzmzhp.service.maintain.LendService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-24 18:14
 * @program: ams
 * @description: 借出记录前端交互类
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/lend")
public class LendController {
    
    @Autowired
    private LendService lendService;

    /**
     * 根据参数查询数据sql
     * @param param 参数
     *        itemId 物品id
     *        itemName 物品名称
     *        stdmodeId  物品类型id
     *        personnelName 借出人名称
     *        borrowerName 借出人名称
     *        lendStatus 借用状态
     *        lendType 借用状态
     *        startTime 开始时间
     *        endTime  结束时间
     * @return 查询数据sql
     */
    @RequestMapping("/findOrnamentsTypeList")
    public ReturnData findLendList(@RequestParam(value = "page", defaultValue = "1") int page,
                                       @RequestParam(value = "limit", defaultValue = "10") int limit,
                                       @RequestBody Map<String,Object> param){
        PageHelper.startPage(page, limit, true);
        List<Lend> list = lendService.findLendByParam(param);
        if (CollUtil.isNotEmpty(list)){
            PageInfo<Lend> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询借出记录信息失败,请重新查询");
    }

    /**
     * 添加借出记录信息
     * @param lend 借出记录信息
     * @return 是否成功
     */
    @RequestMapping("/addStdmode")
    public ReturnData addStdmode(@RequestBody Lend lend){
        Integer i = lendService.addLend(lend);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        }
        return ReturnData.error(ReturnConstant.FAIL, "借出记录添加错误");
    }

    /**
     * 修改借出记录信息
     * @param lend 借出记录信息
     * @return 是否成功
     */
    @RequestMapping("/updateStdmode")
    public ReturnData updateStdmode(@RequestBody Lend lend){
        Integer i = lendService.updateLend(lend);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "借出记录修改错误");
        }
    }

    /**
     * 根据借出记录类型id修改借出记录状态
     * @param lendId 借出记录id
     * @param status 借出记录状态
     * @return 是否成功
     */
    @RequestMapping("/exitProgramType")
    public ReturnData exitStatus(@RequestParam(value = "lendId")Integer lendId,
                                 @RequestParam(value = "status") String status){
        Integer i = lendService.exitStatus(lendId,status);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        }
        return ReturnData.error(ReturnConstant.FAIL, "借出记录状态修改错误");
    }
}
